package com.mtnz.njk.Util;

import cn.hutool.core.util.IdUtil;
import com.mtnz.njk.Mapper.AskMapper;
import com.mtnz.njk.Mapper.CommunityUserMapper;
import com.mtnz.njk.Mapper.NewsMapper;
import com.mtnz.njk.entity.AskEntity;
import com.mtnz.njk.entity.NewsEntity;
import com.mtnz.njk.entity.NjkUserEntity;
import com.mtnz.njk.model.UserAskModel;

import javax.annotation.Resource;

public class ReplyCommentAskReply implements Runnable {
    private String userId;
    private String askId;
    private String message;
    private String replyId;
    private String url;
    @Resource(name = "askMapper")
    private AskMapper askMapper;
    @Resource(name = "userMapper")
    private CommunityUserMapper userMapper;
    @Resource(name = "newsMapper")
    private NewsMapper newsMapper;

    public ReplyCommentAskReply(String userId, String askId, String message, String replyId, AskMapper askMapper, CommunityUserMapper userMapper, NewsMapper newsMapper, String url) {
        this.userId = userId;
        this.askId = askId;
        this.message = message;
        this.replyId = replyId;
        this.askMapper = askMapper;
        this.userMapper = userMapper;
        this.newsMapper = newsMapper;
        this.url = url;
    }


    @Override
    public synchronized void run() {
        try {
            AskEntity askEntity = new AskEntity();
            askEntity.setAskId(askId);
            askEntity.setMessage(message);
            askEntity.setUserId(userId);
            UserAskModel byId = askMapper.findById(askId);
            NjkUserEntity byId1 = userMapper.findById(userId);
            NjkUserEntity byIdOne = userMapper.findByIdOne(replyId);
            //保存消息
            NewsEntity newsEntity = new NewsEntity();
            newsEntity.setNewsId(IdUtil.simpleUUID());
            newsEntity.setId(askEntity.getAskId());
            newsEntity.setMessage(askEntity.getMessage());
            newsEntity.setName(byId1.getNickName());
            newsEntity.setImg(byId1.getImg());
            newsEntity.setCreateDate(cn.hutool.core.date.DateUtil.now());
            newsEntity.setUserId(replyId);
            newsEntity.setSubject(byId.getMessage());
            newsEntity.setStatus("1");
            newsEntity.setUserId1(askEntity.getUserId());
            newsEntity.setUrl(url);
            newsMapper.insert(newsEntity);
            byIdOne.setStatus("1");
            userMapper.updateByPrimaryKeySelective(byIdOne);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
